// ChatClient.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "ChatClient.h"
#include "ChatClientDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CChatClientApp

BEGIN_MESSAGE_MAP(CChatClientApp, CWinApp)
	ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()


// CChatClientApp construction

CChatClientApp::CChatClientApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}


// The one and only CChatClientApp object

CChatClientApp theApp;


// CChatClientApp initialization

BOOL CChatClientApp::InitInstance()
{
//TODO: call AfxInitRichEdit2() to initialize richedit2 library.
	// InitCommonControlsEx() is required on Windows XP if an application
	// manifest specifies use of ComCtl32.dll version 6 or later to enable
	// visual styles.  Otherwise, any window creation will fail.
	INITCOMMONCONTROLSEX InitCtrls;
	InitCtrls.dwSize = sizeof(InitCtrls);
	// Set this to include all the common control classes you want to use
	// in your application.
	InitCtrls.dwICC = ICC_WIN95_CLASSES;
	InitCommonControlsEx(&InitCtrls);

	CWinApp::InitInstance();

	if (!AfxSocketInit())
	{
		AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
		return FALSE;
	}

	AfxInitRichEdit2();

	AfxEnableControlContainer();

	//user authentication
	CString strUserName;
	int nRet = AfxMessageBox(_T("Are you want use PC name as you login?"),MB_YESNOCANCEL);
	switch(nRet)
	{
	case IDYES:
		{
			//get pc name for login
			DWORD dwSize = 1024;
			TCHAR strPCName[1024];
			if(FALSE == GetComputerName(strPCName,&dwSize))
			{
				AfxMessageBox(_T("Can't get PC name"));
				return 0xdead;
			}
			strUserName = strPCName;
		}
		break;
	case IDNO:
		{
			CChCAuthorizationDlg dlg;
			if(IDCANCEL == dlg.DoModal())
				return FALSE;
			strUserName = dlg.UserName();
		}
		break;
	case IDCANCEL:
		return FALSE;
		break;
	default:
		ASSERT(0);
		return FALSE;
		break;
	}
	

	//load connection settings
	CChCSettings::Initialize();


	CChatClientDlg dlg;
	dlg.UserName(strUserName);
	m_pMainWnd = &dlg;
	INT_PTR nResponse = dlg.DoModal();
	if (nResponse == IDOK)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with OK
	}
	else if (nResponse == IDCANCEL)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with Cancel
	}

	CChCSettings::Uninitialize();

	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}
